package com.sizu.web;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sizu.common.basicdto.TGoodsDto;
import com.sizu.common.vo.ResultEntity;
import com.sizu.entity.TGoods;
import com.sizu.service.ITGoodsService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 赵玉玺
 * @since 2020-03-15
 */
@RestController
@RequestMapping("/basic/tGoods")
public class TGoodsController {

    @Resource
    private ITGoodsService itGoodsService;


    /**
     * 查询所有的商品
     *
     * @param tGoodsDto
     * @return
     */
    @RequestMapping("/list")
    public ResultEntity tGoodsList(TGoodsDto tGoodsDto) {
        PageHelper.startPage(tGoodsDto.getPageNum(), tGoodsDto.getPageSize());
        TGoods tGoods=new TGoods();
        BeanUtils.copyProperties(tGoodsDto,tGoods);
        List<TGoods> tGoods1 = itGoodsService.selectGoods(tGoods);
        PageInfo<TGoods> pageInfo = new PageInfo<>(tGoods1);
        return ResultEntity.ok("查询成功", pageInfo);
    }
    /**
     * 添加商品信息
     *
     * @param tGoodsDto
     * @return
     */
    @RequestMapping("/add")
    public ResultEntity tGoodsAdd(TGoodsDto tGoodsDto) {
        TGoods tGoods = new TGoods();
        BeanUtils.copyProperties(tGoodsDto, tGoods);
        itGoodsService.insert(tGoods);
        return ResultEntity.ok("添加成功");
    }

    /**
     * 修改商品信息
     *
     * @param tGoodsDto
     * @return
     */
    @RequestMapping("/up")
    public ResultEntity tGoodsUp(TGoodsDto tGoodsDto) {
        TGoods tGoods = new TGoods();
        BeanUtils.copyProperties(tGoodsDto, tGoods);
        itGoodsService.updateById(tGoods);
        return ResultEntity.ok("修改成功");
    }

    /**
     * 删除商品带批删
     * @param tGoodsDto
     * @return
     */
    @RequestMapping("/del")
    public ResultEntity tGoodsDel(TGoodsDto tGoodsDto){
        for (Integer id : tGoodsDto.getIds()) {
            TGoods tGoods=new TGoods();
            tGoods.setId(id);
            tGoods.setState(0);
            itGoodsService.updateById(tGoods);
        }
        return ResultEntity.ok("删除成功");
    }


}
